package cn.navclub.xt.server.service.impl

import cn.navclub.xt.server.api.CommonResult
import cn.navclub.xt.server.api.RestResponse
import cn.navclub.xt.server.config.USERNAME
import cn.navclub.xt.server.entities.DkpUserEntity
import cn.navclub.xt.server.entities.DkpUserEntityRowMapper
import cn.navclub.xt.server.service.UserService
import cn.navclub.xt.server.util.DBUtil
import io.vertx.kotlin.coroutines.await

class UserServiceImpl : UserService {
    override suspend fun login(username: String, password: String): CommonResult<DkpUserEntity> {
        val template = "SELECT * FROM dkp_user WHERE username=#{username}"
        val query = DBUtil.PreStateQuery.create(USERNAME,username)
        val list = DBUtil.preQuery(DkpUserEntityRowMapper.INSTANCE, template,query).await()
        if (list.isEmpty()||list[0].password!=password){
            return RestResponse.fail("用户名/密码错误!")
        }
        return RestResponse.success(list[0]);
    }
}